<?php
/*
 * 订单详情
 */
namespace Mobile\Controller;

class ServicesController extends BaseController {

    public $servicesModel;

    public function __construct() {
        parent::__construct();
        $this->servicesModel = M('services');
    }
    
    
    /*
     * 生成支付订单
     */
    public function pay(){
        
        $date1 = C("FUWUQI_DAOQI");
        
        $date2 = date("Y-m-d H:i:s");
        $per_money = C("FUWUQI_PERMONEY");
         
        $day = get_daynums($date2,$date1);

		if($day>0){
			$day += 365;
		}else{
			$day = 365;
		}

        $order_title = "服务器续期365天";
        
        $pay_money = $per_money * $day;
		
		$pay_money = 3000;//一万一年
		
		
        
        $pay_money = 0.01;
        
        $order_sn = "S".date("YmdHis").rand(100,999);//订单号
        
        $order_data = array(
            "pay_money" => $pay_money,
            "order_title" => $order_title,
            "create_time" => $date2,
            "checked" => 0,
            "order_sn" => $order_sn,
            "order_ip" => get_client_ip(),
            "days" => $day
        );
        
        $id = $this->servicesModel->add($order_data);
        if(!$id){
            $this->error2("创建续费订单失败");
        }
        
        $this->alipay($id);
        
    }
    
    
    public function sms(){
        $money = get('money');
        if($money<=0) {
            exit('error');
        }
        $order_title = "短信充值";
        $pay_money = (int)$money;
        //$pay_money = 0.01;
        $order_sn = "S".date("YmdHis").rand(100,999);//订单号
        $order_data = array(
            "pay_money" => $pay_money,
            "order_title" => $order_title,
            "create_time" => $date2,
            "checked" => 0,
            "order_sn" => $order_sn,
            "order_ip" => get_client_ip(),
            "days" => ''
        );
        $id = $this->servicesModel->add($order_data);
        if(!$id){
            $this->error2("创建续费订单失败");
        }
        $this->alipay($id);
    }


    /*
     * 支付宝支付
     */
    public function alipay($id=''){
        
        if($id){
            $order_id = $id;
        }else{
            $order_id = get('order_id');
        }
        
        if(!$order_id){
            $this->error2("订单参数错误");
        }
    
        $where = array(
            "order_id" => $order_id,
        );
        $order = $this->servicesModel->where($where)->find();
        if(!$order){
            $this->error2("订单未找到");
        }
        
        if($order['checked'] != 0){
            $this->error2("当前订单已完结,不能支付");
        }
        
        $alipay = new PayalipcController();
        
        $data['notify_url'] = get_domain().'/mobile/services/alipaycallback/order_id/'.$order_id;//异步通知
    
        $call_url = get_domain().'/admin//index/cangku/order_id/'.$order_id;//同步通知
        
        $data['return_url'] = $call_url;
    
        //用户付款中途退出返回商户网站的地址
        $data['show_url'] = $call_url;
    
        $data['out_trade_no'] = $order['order_sn'];//商城订单号
        
        $data['subject'] = $order['order_title'];//商品名称
        
        // $data['body'] = '';//商品描述
        $data['total_fee'] = $order['pay_money'];//交易金额
        
        $url = $alipay->wappay($data);
    
        header("Location:".$url);exit;
    
    
    }
    
    
    /*
     * 支付宝回调
     */
    public function alipaycallback(){
    
        $order_id = get('order_id');
        if(!$order_id){
            echo "fail";exit;
        }
    
        $where = array(
            "order_id" => $order_id,
        );
        //查找订单
        $order = $this->servicesModel->where($where)->find();
        if(empty($order)){
            echo "fail";exit;
        }
    
        $data = $_POST;
       // file_put_contents('alipay.txt', json_encode($data));
        $alipay = new PayalipcController();
        $res = $alipay->checkSign($data);
    
        if($res == FALSE){
            echo "fail";exit;
        }else{
            //签名正确
            echo "success";
        }
    
        if($res){
            if ($data["trade_status"] == "TRADE_SUCCESS") {
                //更改订单状态
                if($order['checked'] == 0){
                    //待支付,更改状态为已支付
                    $this->_alipaychangeOrder($order,$data);//更改订单状态为已经支付
                }
            }
        }
    }
    
    //支付成功,更改订单状态
    protected function _alipaychangeOrder($order,$data){
        
        $where = array(
            "order_id" => $order['order_id']
        );
        
        $order_data = array(
            "checked" => 2,//已完成
            "pay_time" => $data['gmt_payment'],
            'trade_no' => $data['trade_no'],
            'buyer_email' => $data['buyer_email'],//买家支付宝账号
            'buyer_id' => $data['buyer_id'],//买家支付宝id
        );
         
        $order_res = $this->servicesModel->where($where)->save($order_data);
        if($order_res){
           //支付成功,
            if($order['pay_money']>=500) {
                $num = (int)($order['pay_money']/0.05);
            }else{
                $num = (int)($order['pay_money']/0.06);
            }
            $where_c = array(
                "cname" => "SMS_NUM"
            );
            $res = M("config")->where($where_c)->setInc('cvalue',$num);
           return true;
        }
        
        return false;
        
    }
    
}